package com.xj.admin.bussiness.machineryInfo.web;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.Validator;
import com.feilong.core.bean.ConvertUtil;
import com.xj.admin.base.index.web.BaseController;
import com.xj.admin.bussiness.machineryInfo.service.ITbMachineryInfoService;
import com.xj.admin.bussiness.qrext4j.src.main.java.org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
import com.xj.admin.util.SessionUtil;
import com.xj.admin.util.dtgrid.model.Pager;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.exception.EnumSvrResult;
import com.xj.common.base.common.util.JsonUtil;
import com.xj.common.bussiness.machinery.entity.TbMachineryInfo;
import com.xj.common.bussiness.user.entity.TbUser;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

import static com.xj.admin.bussiness.WaterMaker.ImageMarkLogoByIcon.markImageByMoreText;

/**
 * <p>
 * 机械信息管理 <b/>
 * 前端控制器
 * </p>
 *
 * @author zhaohai
 * @since 2017-12-29
 */
@Controller
@RequestMapping("/machineryInfo/")
public class TbMachineryInfoController extends BaseController {

    @Autowired
    private ITbMachineryInfoService machineryInfoService;

    @GetMapping("listUI")
    public String listUI() {
        return "machineryInfo/list";
    }

    @SuppressWarnings("unchecked")
    @PostMapping("list")
    @ResponseBody
    public Object list(String gridPager) {
        Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
        Map<String, Object> parameters = null;
        if (Validator.isNullOrEmpty(pager.getParameters())) {
            parameters = new HashMap<>();
        } else {
            parameters = pager.getParameters();
        }
        Page<TbMachineryInfo> list = machineryInfoService.selectPage(new Page<TbMachineryInfo>(pager.getNowPage(), pager.getPageSize()), Condition.create().allEq(parameters).orderBy("id", false));
        makeGridResult(parameters, pager, list);
        return parameters;
    }

    @GetMapping("form")
    public String form(Map<String, Object> map) {
        return "machineryInfo/form";
    }

    @GetMapping("QrcodeForm")
    public String QrcodeForm(Map<String, Object> map) {
        return "machineryInfo/QrcodeForm";
    }

    @RequestMapping(value = "all", method = RequestMethod.POST)
    @ResponseBody
    public AbstractBean all(String phone) {
        AbstractBean bean = new AbstractBean();
        List<TbMachineryInfo> list = machineryInfoService.selectByMap(ConvertUtil.toMap("phone", phone));

        if (list == null) {
            bean.setStatus(EnumSvrResult.ERROR.getVal());
            bean.setMessage(EnumSvrResult.ERROR.getName());
        } else {
            bean.setData(list);
        }
        return bean;
    }


    @PostMapping("save")
    @ResponseBody
    public AbstractBean add(TbMachineryInfo tbmachineryInfo) {
        AbstractBean bean = new AbstractBean();
        if (tbmachineryInfo.getId() == null) {
            tbmachineryInfo.setBuyDate(new Date());
            tbmachineryInfo.setDateOfProduction(new Date());
        }
        if (!machineryInfoService.insertOrUpdate(tbmachineryInfo)) {
            bean.setStatus(EnumSvrResult.ERROR.getVal());
            bean.setMessage(EnumSvrResult.ERROR.getName());
        }
        return bean;
    }

    @DeleteMapping("{id}/delete")
    @ResponseBody
    public AbstractBean delete(@PathVariable(required = true) Integer id) {
        if (!machineryInfoService.deleteById(id)) {
            return fail(EnumSvrResult.ERROR);
        }
        return success();
    }

    @GetMapping("{id}/select")
    public String select(Map<String, Object> map, @PathVariable(required = true) Integer id) {
        TbMachineryInfo tbMachineryInfo = machineryInfoService.selectById(id);
        map.put("record", tbMachineryInfo);
        return "machineryInfo/edit";
    }

    public String selectIdentityNum(Map<String, Object> map, @PathVariable(required = true) String identityNum) {
        if (!identityNum.equals("") && identityNum != null) {
            TbMachineryInfo tbMachineryInfo = machineryInfoService.selectIdentityNum(identityNum);
            if (tbMachineryInfo != null) {
                map.put("record", tbMachineryInfo);
                return "machineryInfo/QrcodeForm";
            } else {
                return "您的身份证号码不存在";
            }
        } else {
            return "请输入身份号码";
        }

    }

    @GetMapping("{id}/info")
    public String info(Map<String, Object> map, @PathVariable(required = true) Integer id) {
        TbMachineryInfo tbMachineryInfo = machineryInfoService.selectById(id);
        map.put("record", tbMachineryInfo);
        return "machineryInfo/info";
    }

    @DeleteMapping("{ids}/deleteBatch")
    @ResponseBody
    public AbstractBean deleteBatch(@PathVariable(required = true) String ids) {
        if (!machineryInfoService.deleteBatchIds(ConvertUtil.toList(ids.split(",")))) {
            return fail(EnumSvrResult.ERROR);
        }
        return success();
    }

    @GetMapping("{id}/printPreview")
    public String printPreview(Map<String, Object> map, @PathVariable(required = true) Integer id) throws IOException {
        TbMachineryInfo tbMachineryInfo = new TbMachineryInfo();
        tbMachineryInfo.setId(id);
        List<TbMachineryInfo> machineryInfoList = machineryInfoService.searchMachineryInfoList(tbMachineryInfo);
        //System.out.println("++++machineryInfoList++++="+machineryInfoList);
        for (int i = 0;i<machineryInfoList.size();i++){
            machineryInfoList.get(i).getOwnerName();
            machineryInfoList.get(i).getBigShelfNum();
            machineryInfoList.get(i).getIdentityNum();
            String content = "您的档案证所有人="+machineryInfoList.get(i).getOwnerName()+"，大架号="+machineryInfoList.get(i).getBigShelfNum()+",身份证号="+machineryInfoList.get(i).getIdentityNum();
            boolean success = new SimpleQrcodeGenerator().setLogo("F:\\二维码\\YZlogo.jpg"/*"/usr/docker/images/Qrcorde/logo/YZlogo.jpg"*/).
                    generate(content).toFile("F:\\二维码\\Qrcode.jpg"/*"/usr/docker/images/Qrcorde/qrcode/qrcode.jpg"*/);
            Assert.assertTrue(success);
            System.out.println("+++++++++++++++++二维码添加完成");
        }
        map.put("machineryInfoList", machineryInfoList);
        return "machineryInfo/printPreview";
//        TbUser user = SessionUtil.getloginUser();
        //System.out.println("当前登录用户信息=" + user.toString());
//        String logoText = user.getUserName();
//        String srcImgPath = "F:/ye/backimg.jpg";/*"/usr/docker/images/cms-admin-background/srcImgPath/backimg.jpg";*/
//        String targerPath = "F:/ye/wm.jpg";/*"/usr/docker/images/cms-admin-background/targerTextPath/more.jpg";*/
//        markImageByMoreText(logoText, srcImgPath, targerPath);
//        ImageMarkLogoByIcon.markImageByText(logoText, srcImgPath, targerTextPath);
//        System.out.println("-----------------水印添加完成");

    }
}